package com.jfs.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jfs.constant.SqlConstant;
import com.jfs.entity.ChannelDirectory;
import com.jfs.entity.dto.DeviceChannelListDto;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Set;

/**
 * @Description 设备目录操作dao层
 * @E-mail cwm3412638@163.com
 * @Author cwm
 * @Date 2022/8/10
 */
@Mapper
public interface ChannelDirectoryMapper extends BaseMapper<ChannelDirectory> {
    /**
     * 查询当前用户拥有目录的顶级父目录
     *
     * @param userId
     * @return
     */
    @Select("SELECT tcd.id, tcd.name FROM t_channel_directory tcd LEFT JOIN t_middle_user_channel_directory tmucd ON tcd.id = tmucd.channel_directory_id WHERE tmucd.user_id = #{userId} AND tcd.level = ( SELECT min(level) AS level FROM ( SELECT tcd.*, tmucd.* FROM t_channel_directory tcd LEFT JOIN t_middle_user_channel_directory tmucd ON tcd.id = tmucd.channel_directory_id WHERE tmucd.user_id = #{userId} GROUP BY tcd.level ) ) order by tcd.sort")
    List<ChannelDirectory> channelDirectoryByUserIdAndTop(@Param("userId") String userId);

    /**
     * 根据当前用户拥有目录查询下级目录
     *
     * @param userId
     * @param parentId
     * @return
     */
    @Select("SELECT tcd.id, tcd.name FROM t_channel_directory tcd LEFT JOIN t_middle_user_channel_directory tmucd ON tcd.id = tmucd.channel_directory_id WHERE tmucd.user_id = #{userId} AND tcd.parent_id = #{parentId}  order by  tcd.sort")
    List<ChannelDirectory> channelDirectoryByUserIdAndParentId(@Param("userId") String userId, @Param("parentId") String parentId);

    /**
     * 根据目录id条件查询目录下的通道列表
     * @param page
     * @param channelDirectoryIds
     * @param searchKey
     * @param enable
     * @return
     */
    @Select(SqlConstant.BASE_SCRIPT_START +
            "select  tdc.* from t_device_channel tdc inner join t_middle_channel_directory tmcd on tdc.channel_id = tmcd.channel_id"
            + SqlConstant.WHERE_START
            +"<if test=\"channelDirectoryIds != '' and channelDirectoryIds.size>0\">"
            +"tmcd.channel_directory_id in"
            +"<foreach collection=\"channelDirectoryIds\" index=\"index\" item=\"channelDirectoryId\" open=\"(\" separator=\",\" close=\")\">"
            +"#{channelDirectoryId}"
            +"</foreach>"
            + SqlConstant.IF_END
            + "<if test= \"searchKey != '' and searchKey!=null \">"
            + "and tdc.name like #{searchKey}"
            + "or tdc.channel_id like #{searchKey}"
            + "or tdc.manufacturer like #{searchKey}"
            + SqlConstant.IF_END
            + "<if test=\"enable != null\">"
            +"and tdc.status = #{enable}"
            +SqlConstant.IF_END
            +SqlConstant.WHERE_END
            +SqlConstant.BASE_SCRIPT_END
    )
    IPage<DeviceChannelListDto> pageListChannelByDirectory(Page page, @Param("channelDirectoryIds") Set<String> channelDirectoryIds, @Param("searchKey") String searchKey, @Param("enable")Boolean enable);
}
